import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Box 		= $Lazy.load(()=>import('#tp/box/box'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		formData : [
			{ label: '换算前单位',		name: 'conv_before',	full:true, required:true, type:'select', idStr:'code' },
			{ label: '换算后单位',		name: 'conv_after',		full:true, required:true, type:'select', idStr:'code' },
			{ label: '换算单位系数',		name: 'coefficient',	full:true, required:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		const { kind_id } = this.props
		this.query = { kind_id }
		this.submit = { check_result_id: kind_id }
		this.fetch()
	}
	// 获取弹框下拉
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, 'conv_before', ()=>{
			$cache.then( async f => {
				const { data } = this.props
				$fn.setRow( formData, 'conv_before', data)
				$fn.setRow( formData, 'conv_after', data)
				//
				this.setState({ formData })
			})
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'unit-conversion/index')
	
	// 表单
	cols = [
		{ title: '换算前单位',		dataIndex: 'conv_before_name', 		width:150 },
		{ title: '换算后单位', 		dataIndex: 'conv_after_name', 		width:150 },
		{ title: '单位转换系数', 	dataIndex: 'coefficient', 			width:250},
		TableControls.controls.call(this,{ width: 80 })
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		const { disabled } = this.props
		return [
			TableControls.edit.call(this,'unit-conversion/info', { uuid }, {
				click:this.getModalSelect,
				ids: ({ uukd }) => ({ check_result_id: this.props.kind_id, uuid  }),		// 重定义参数
				disabled
			}),
		]
	}
	// 操作按钮
	controls = () => {
		const { disabled } = this.props
		return [
			Controls.add.call(this,{click:this.getModalSelect, code:false, disabled}),
			Confirm.call(this,'删除', 'unit-conversion/del', { isKeys: true, ghost:true })
		]
	}
	render(){
		const title = '单位换算系数'
		const { disabled } = this.props
		return (
			<>
				<Box key={disabled} title={title} controls={this.controls()} className='mt5' contentClassName='pt10'>
					{/* 表格 */}
					{ Table.call(this, { wrapStyle: this.props.tableStyle, disabled }) }
				</Box>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ title, labelWidth:'s7', api: 'unit-conversion/add', editApi: 'unit-conversion/edit' }) }
			</>
		)
	}
}

export default Index